package com.baizhi.cmfz.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpResponse;
import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.annottation.LogAnnottion;
import com.baizhi.cmfz.entity.User;
import com.baizhi.cmfz.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/selectUser")
    public  String  userLogin(User user,HttpSession session,String code){
        System.out.println("8888");
        LineCaptcha lineCaptcha = (LineCaptcha) session.getAttribute("lineCaptcha");
        System.out.println("99999");
        String c = code.toLowerCase();
        boolean verify = lineCaptcha.verify(c);
        System.out.println("-----------"+lineCaptcha.getCode());
        System.out.println("---------"+code);
        if(verify){
            //数据封装token
            UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(),user.getPassword());
            //获取Subject 对象主体
            Subject subject = SecurityUtils.getSubject();
            try{
                User user1=new User();
                user1.setUserName(user.getUserName());
                user1.setPassword(user.getPassword());
                System.out.println(user+"controller user");
                //调用登录方法
                subject.login(token);
                session.setAttribute("user",user1);
                return  "redirect:/main.jsp";
            }catch (Exception e){
                return  "redirect:error.jsp";

            }
//            User user1 = userService.selectUser(user);
//            if(user1 != null){
//                System.out.println("222222222222");
//                session.setAttribute("user",user1);
//                return  "redirect:main.jsp";
//            }else{
//                return  "redirect:error";
//            }

        }else {
            return "验证码为空";
        }


    }
    @RequestMapping("/getCode")
    public  void  Code(HttpServletResponse response, HttpSession session) throws IOException {


        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);

//图形验证码写出，可以写出到文件，也可以写出到流
        lineCaptcha.write(response.getOutputStream());
    //输出code
        Console.log(lineCaptcha.getCode());
        session.setAttribute("lineCaptcha",lineCaptcha);

    }
    @ResponseBody
    @RequestMapping("/selectByPageUser")
    @LogAnnottion(type = "select", value = "查询管理员")
    @AddCacheAnnotation
    public Map selectByPageUser(Integer page,Integer limit){

        return userService.selectByPageUser(page,limit);
    }
    @RequestMapping("/insertUser")
    @ResponseBody
    @LogAnnottion(type = "insert", value = "添加管理员")
    @DeleteCacheAnnotation
    public  Map insertUser(User user){

        return  userService.insertUser(user);
    }
    @RequestMapping("/deleteUser")
    @ResponseBody
    @DeleteCacheAnnotation
    public  Map deleteUser(int id){
        return  userService.deleteUser(id);
    }
    @ResponseBody
    @RequestMapping("/selectOneUser")
    @LogAnnottion(type = "select", value ="查询一个管理员")
    @AddCacheAnnotation
    public  User selectOneUser(int id){
        return  userService.selectOneUser(id);
    }
    @RequestMapping("/updateUser")
    @ResponseBody
    @LogAnnottion(type = "update", value = "修改管理员")
    public  Map updateUser(User user){
        System.out.println(user+"---------------------------");
       return  userService.updateUser(user);
    }
}
